package net.lzzy.practices.web.courses;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Massages;
import net.lzzy.practices.entities.Applicant;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ApplicantService;
import net.lzzy.practices.utils.StringUtil;
import net.lzzy.practices.web.models.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.IllegalFormatException;
import java.util.List;
import java.util.UUID;
@Api(value = "报名相关接口")
@RestController
@RequestMapping(ApiConstant.ROUTE_APPLY_ROOT)
@CrossOrigin
public class ApplicantApiController {
    private final ApplicantService service;

    public ApplicantApiController(ApplicantService service) {
        this.service = service;
    }

    @ApiOperation("报名")
    @ApiImplicitParam(name = "info",value = "报名信息",dataType = "ApplicantInfo",dataTypeClass = ApplicantInfo.class,paramType = "body",required = true)
    @PostMapping(ApiConstant.ROUTE_APPLY_APPLY)
    public ApiResult apply(@RequestBody ApplicantInfo info) {
        String token=info.getToken();
        String strCourseId=info.getCourseId();
        String strStudentId=info.getStudentId();
        if (StringUtil.isEmpty(token)||StringUtil.isEmpty(strCourseId)||StringUtil.isEmpty(strStudentId)){
            return new ApiResult(false, Massages.MISS_ARGS.toString(), null);
        }
        try {
            UUID studentId =UUID.fromString(strStudentId);
            UUID courseId =UUID.fromString(strCourseId);
            Applicant applicant=service.apply(studentId,courseId,token);
            if (applicant==null){
                return  new ApiResult(false,Massages.WRONG_ID.toString(), null);
            }
            return new ApiResult(true,Massages.WRONG_ID.toString(), new VmApplicant(applicant));
        }catch (IllegalFormatException e){
            return new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }
    }
    @ApiOperation("查询学生报名课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "学生id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_COURSES)
    public ApiResult getStudentApplicants(@RequestParam(name = "id") UUID studentId,@RequestParam String token){
        List<Applicant> applicants =service.getAllCourses(studentId,token);
        return transform(applicants);
    }
    private static ApiResult transform(List<Applicant> applicants) {
        if (applicants ==null){
            return new ApiResult(false, Massages.NO_PERMISSION.toString(), null);
        }
        List<VmApplicant> vmApplicants =new ArrayList<>();
        applicants.forEach(applicant -> vmApplicants.add(new VmApplicant(applicant)));
        return new ApiResult(true, applicants.size() + "", vmApplicants);
    }
    @ApiOperation("查询学生已报名课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "学生id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_ENROLLED)
    public ApiResult getEnrolledApplicants(@RequestParam(name = "id") UUID studentId,@RequestParam String token){
        List<Applicant> applicants=service.getEnrolledCourses(studentId,token);
        return transform(applicants);
    }
    @ApiOperation("查询学生申请课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "学生id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_APPLYING)
    public ApiResult getApplyingApplicants(@RequestParam(name = "id") UUID studentId,@RequestParam String token){
        List<Applicant> applicants=service.getApplyingCourses(studentId,token);
        return transform(applicants);
    }
    @ApiOperation("查询学生已报名课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "课程id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_APPLY_STUDENT_DECLINED)
    public ApiResult getDeclinedCourses(@RequestParam(name = "id") UUID courseId,@RequestParam String token){
        List<Applicant> applicants=service.getDeclinedCourses(courseId,token);
        return  transform(applicants);
    }
    @ApiOperation("审批报名申请")
    @ApiImplicitParam(name = "info",value = "报名信息",dataType = "ApproveInfo",dataTypeClass = ApproveInfo.class,paramType = "body",required = true)
    @PostMapping(ApiConstant.ROUTE_APPLY_APPROVE)
    public ApiResult approve(@RequestBody ApproveInfo info){
        String token=info.getToken();
        String strApplicantId=info.getApplicantId();
        if (StringUtil.isEmpty(token)||StringUtil.isEmpty(strApplicantId)){
            return new ApiResult(false,Massages.MISS_ARGS.toString(), null);
        }
        try {
            UUID applicantId=UUID.fromString(strApplicantId);
            Applicant applicant=service.approve(applicantId,token);
            if (applicant==null){
                return  new ApiResult(false,Massages.NO_PERMISSION.toString(), null);
            }
            return new ApiResult(true,Massages.SUCCESS.toString(), new VmApplicant(applicant));

        }catch (IllegalFormatException e){
            return new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }
    }
    @ApiOperation("审批报名申请")
    @ApiImplicitParam(name = "info",value = "报名信息",dataType = "ApproveInfo",dataTypeClass = ApproveInfo.class,paramType = "body",required = true)
    @PostMapping(ApiConstant.ROUTE_APPLY_DECLINE)
    public ApiResult decline(@RequestBody ApproveInfo info){
        String token=info.getToken();
        String strApplicantId=info.getApplicantId();
        if (StringUtil.isEmpty(token)||StringUtil.isEmpty(strApplicantId)){
            return new ApiResult(false,Massages.MISS_ARGS.toString(), null);
        }
        try {
            UUID applicantId=UUID.fromString(strApplicantId);
            Applicant applicant=service.decline(applicantId,token);
            if (applicant==null){
                return  new ApiResult(false,Massages.NO_PERMISSION.toString(), null);
            }
            return new ApiResult(true,Massages.SUCCESS.toString(), new VmApplicant(applicant));

        }catch (IllegalFormatException e){
            return new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }
    }
    @ApiOperation("获取学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "课程id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_APPLY_TEACHER_APPLYING)
    public ApiResult getApplyingStudents(@RequestParam(name = "id") UUID courseId,@RequestParam String token){
        List<Applicant> applicants=service.getApplyingStudents(courseId,token);
        return  transform(applicants);
    }
    @ApiOperation("获取学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "课程id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_APPLY_TEACHER_ENROLLED)
    public  ApiResult getEnrolledStudents(@RequestParam(name = "id") UUID courseId,@RequestParam String token){
        List<Applicant> applicants=service.getEnrolledStudents(courseId,token);
        return  transform(applicants);
    }
    @ApiOperation("获取学生列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "课程id",dataType = "String",dataTypeClass = String.class,paramType = "path", required = true),
            @ApiImplicitParam(name = "token",value = "用户token",dataType = "String",dataTypeClass = String.class,paramType = "query", required = true)
    })
    @GetMapping(ApiConstant.ROUTE_APPLY_TEACHER_DECLINED)
    public ApiResult getDeclinedStudents(@RequestParam(name = "id") UUID courseId,@RequestParam String token){
        List<Applicant> applicants=service.getDeclinedStudents(courseId,token);
        return  transform(applicants);
    }
    @ApiOperation("移除学生")
    @ApiImplicitParam(name = "info",value = "报名信息",dataType = "ApplicantInfo",dataTypeClass = ApplicantInfo.class,paramType = "body",required = true)
    @PostMapping(ApiConstant.ROUTE_APPLY_REMOVE)
    public ApiResult remove(@RequestParam ApplicantInfo info){
        String token=info.getToken();
        String strCourseId=info.getCourseId();
        String strStudentId=info.getStudentId();
        if (StringUtil.isEmpty(token)||StringUtil.isEmpty(strCourseId)||StringUtil.isEmpty(strStudentId)){
            return new ApiResult(false,Massages.MISS_ARGS.toString(),null );
        }
        try {
            UUID courseId=UUID.fromString(strCourseId);
            UUID studentId=UUID.fromString(strStudentId);
            Course course=service.removeStudent(courseId,studentId,token);
            if (course==null){
                return  new ApiResult(false,Massages.NO_PERMISSION.toString(), null);
            }
            return new ApiResult(true,Massages.SUCCESS.toString(), VmCourse.create(course,true));
        }catch (IllegalFormatException e){
            return  new ApiResult(false,Massages.WRONG_ID.toString(), null);
        }
    }
}
